// 辅助函数获取state
export function mapState(useStore, keysOrMapper) {
  return Array.isArray(keysOrMapper) ? keysOrMapper.reduce((reduced, key) => {
    // ...mapState(useStore, ['count']) 取法
    reduced[key] = function () {
      // 实例->取值
      return useStore()[key];
    }
    return reduced;
  }, {}) : Object.keys(keysOrMapper).reduce((reduced, key) => {
    // ...mapState(useStore, {f: 'fruits'}) 取法
    reduced[key] = function () {
      // 实例
      const store = useStore();
      // 真正的key名
      const storeKey = keysOrMapper[key];
      return store[storeKey];
    }
    return reduced;
  }, {})
}
// 辅助函数获取action
export function mapActions(useStore, keysOrMapper) {
  return Array.isArray(keysOrMapper) ? keysOrMapper.reduce((reduced, key) => {
    reduced[key] = function (...args) {
      // 实例->取值
      return useStore()[key](...args);
    }
    return reduced;
  }, {}) : Object.keys(keysOrMapper).reduce((reduced, key) => {
    reduced[key] = function (...args) {
      // 实例
      const store = useStore();
      // 真正的key名
      const storeKey = keysOrMapper[key];
      return store[storeKey](...args);
    }
    return reduced;
  }, {})
}
// 辅助函数获取可读写的state,就是比mapState多个get/set
export function mapWritableState(useStore, keysOrMapper) {
  return Array.isArray(keysOrMapper) ? keysOrMapper.reduce((reduced, key) => {
    reduced[key] = {
      // 实例->取值
      get() {
        return useStore()[key];
      },
      set(value) {
        useStore()[key] = value;
      }
    }
    return reduced;
  }, {}) : Object.keys(keysOrMapper).reduce((reduced, key) => {
    reduced[key] = {
      get() {
        // 实例
        const store = useStore();
        // 真正的key名
        const storeKey = keysOrMapper[key];
        return store[storeKey];
      },
      set(value) {
        // 实例
        const store = useStore();
        // 真正的key名
        const storeKey = keysOrMapper[key];
        store[storeKey] = value;
      }
    }
    return reduced;
  }, {})
}